package microarray;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedList;
import java.util.Map;

import org.xml.sax.helpers.DefaultHandler;

import com.google.appengine.api.urlfetch.FetchOptions;
import com.google.appengine.api.urlfetch.HTTPMethod;
import com.google.appengine.api.urlfetch.HTTPRequest;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
import com.google.appengine.api.urlfetch.FetchOptions.Builder;


public abstract class EUtil extends DefaultHandler  {
	public InputStream openInput(String baseUrl, Map<String, String> params) throws IOException 
	{
    	String urlString = baseUrl + "db=gds&"+ MicroArrayServlet.TOOL;
    	for (Map.Entry<String, String> param: params.entrySet())
    	{
    		urlString += "&" + param.getKey() + "=" + param.getValue();
    	}
    	URL url = new URL(urlString);
		
		FetchOptions fetch = Builder.allowTruncate();
		URLFetchService fs = URLFetchServiceFactory.getURLFetchService(); 
		
    	HTTPResponse resp = fs.fetch(new HTTPRequest( url, HTTPMethod.GET, fetch.allowTruncate().setDeadline(15.0)));
    	ByteArrayInputStream bytein = new ByteArrayInputStream(resp.getContent());
    	
    	return bytein;
	}
	
	public abstract void parseData(InputStream in) throws Exception;
}
